﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class TranslateInterface : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            else
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                if (ou.UserAccessLevel == "0" || ou.UserAccessLevel == "1")
                {
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=" + ou.UserAccessLevel);
                }
                
                if (ou.TeamId == "Null")
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=274ggdizz5!lk");

                string Language = GND.ReadNivdalIntData("UserId", "Nivdal_GetUserTranslateLanguage", ou.UserId);
                if ((Language == null) || (Language == "0"))
                    Response.Redirect("GeneralMessage.aspx?MessageString=k882m299nnqmhtsd");
                else if (Language == "2")
                    Response.Redirect("TranslateEnglish.aspx");
                else
                {
                    this.LanguageHiddenField.Value = Language;
                    if (!IsPostBack)
                    {
                        TranslateDataSource.SelectParameters.Add("UserId", ou.UserId);
                        TranslationStatusDataSourse.SelectParameters.Add("Language", Language);
                    }
                }
            }
        }

        protected override void InitializeCulture()
        {

            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string TranslatorString(object LanguageName)
        {
            return (Resources.Resource.Translate_YouTranslateToLabelText + " " + LanguageName.ToString());
        }

        protected void TranslateSentence(object sender, FormViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = "";
            string SentenceId = (TranslateFormView.FindControl("SentenceIdHiddenField") as HiddenField).Value;

            if (e.CommandName == "Translate")
            {
                string Text = (TranslateFormView.FindControl("TranslationTextBox") as TextBox).Text.Replace("\r\n", "<br />");
                if (Text.Length > 0)
                    Answer = GND.ReadNivdalStringData("Language", "SentenceId", "Text", "Nivdal_TranslateInterfaceSentence", LanguageHiddenField.Value, SentenceId, Text);
            }
            else if (e.CommandName == "Skip")
            {
                Answer = GND.ReadNivdalStringData("UserId", "SentenceId", "Nivdal_IgnoreInterfaceSentence", ou.UserId, SentenceId);
            }
            MessageLabel.Text = Answer;
            TranslateFormView.DataBind();
            TranslationStatusForumView.DataBind();
        }
    }
}